<?php

declare (strict_types=1);

namespace app\store\model\customer;

/**
 * Class Customer
 * @package app\store\model\customer
 */
class Customer extends \app\common\model\customer\Customer
{
    /**
     * 检索查询条件
     * @param array $param
     * @return array
     */
    private function getFilter(array $param = []): array
    {
        // 默认查询条件
        $param = $this->setQueryDefaultValue($param, [
            'search' => '',
            'customer_type' => -1,
            'customer_identity' => -1,
            'corporate_type' => -1,
            'create_time' => [],
        ]);
        // 检索查询条件
        $filter = [];
        !empty($param['search']) && $filter[] = ['full_name|idnumber|corporate_name|signatory|mobile', 'like', "%{$param['search']}%"];
        $param['customer_type'] > -1 && $filter[] = ['customer_type', '=', $param['customer_type']];
        $param['customer_identity'] > -1 && $filter[] = ['customer_identity', '=', $param['customer_identity']];
        $param['corporate_type'] > -1 && $filter[] = ['corporate_type', '=', $param['corporate_type']];

        if (!empty($param['create_time'])) {
            $times = between_time($param['create_time']);
            $filter[] = ['create_time', '>=', $times['start_time']];
            $filter[] = ['create_time', '<', $times['end_time'] + 86400];
        }
        return $filter;
    }

    /**
     * 获取列表记录
     * @param array $param
     * @return \think\Paginator
     * @throws \think\db\exception\DbException
     */
    public function getList(array $param = [], array $store = []): \think\Paginator
    {
        // 检索查询条件
        $filter = $this->getFilter($param);

        if ($store['user']['is_super'] != 1) {
            $filter[] = ['store_user_id', '=', $store['user']['store_user_id']];
        }
        // 查询列表数据
        return $this->where($filter)
            ->with(['store_user','source'])
            ->where('is_delete', '=', 0)
            ->order(['create_time' => 'desc'])
            ->paginate(15);
    }

    public function all(array $param = [], array $store = [])
    {
        // 检索查询条件
        $filter = $this->getFilter($param);

        if ($store['user']['is_super'] != 1) {
            $filter[] = ['store_user_id', '=', $store['user']['store_user_id']];
        }
        // 查询列表数据
        return $this->where($filter)
            ->with(['store_user'])
            ->where('is_delete', '=', 0)
            ->order(['create_time' => 'desc'])
            ->select();
    }


    public function createData($data)
    {
        if (!empty($data['region'])) {
            $data['region'] = implode(', ', $data['region']);
        }
        return $data;
    }

    /**
     * 添加新记录
     * @param array $data
     * @param array $store
     * @return bool|false
     */
    public function adds(array $data, array $store = []): bool
    {
        if ($data['customer_identity'] == 0) {
            $data['signatory'] = $data['full_name'];
        }

        $data['create_time'] = time();
        $data['store_user_id'] = $store['user']['store_user_id'];
        $data['store_id'] = self::$storeId;

        $this->transaction(function () use ($data, $store) {
            $this->save($this->createData($data));
            //生成本次套餐
            $packageModel = new Package;
            $packageModel->save([
                'customer_id' => $this->customer_id,
                'service_desc' => $data['service_desc'],
                'pay_type' => $data['pay_type'],
                'store_user_id' => $store['user']['store_user_id'],
                'store_id' => self::$storeId,
                'create_time' => time()
            ]);

            //生成本次套餐服务内容
            if (!empty($data['serviceTable'])) {
                (new Service())->saveList($this->customer_id, $packageModel->package_id, $data['serviceTable']);
            }

            //生成合同
            $packageModel->generateContract();
        });
        return true;
    }


    /**
     * 更新记录
     * @param array $data
     * @return bool
     */
    public function edits(array $data): bool
    {
        return $this->save($this->createData($data)) !== false;
    }

    /**
     * 删除记录 (软删除)
     * @return bool
     */
    public function setDeleted(): bool
    {
        //删除关联套餐
        (new Package())->where(['is_delete' => 0, 'customer_id' => $this->customer_id])->save(['is_delete' => 1]);
        //删除关联套餐内容
        (new Service())->where(['is_delete' => 0, 'customer_id' => $this->customer_id])->save(['is_delete' => 1]);
        return $this->save(['is_delete' => 1]) !== false;
    }
}
